#ifndef __MALLOC_ALIGN_H__
#define __MALLOC_ALIGN_H__

#include <malloc.h>

inline void* malloc_align(size_t size, unsigned int log2_align) {
    void* ret;

    unsigned long align = 1 << log2_align;
    char* real = (char *)malloc(size + sizeof(void *) + (align-1));
    if (real) {
        unsigned long offset = (align - (unsigned long)(real + sizeof(void *))) & (align-1);
        ret = (void *)((real + sizeof(void *)) + offset);
        *((void **)ret - 1) = (void *)real;
    } else {
        ret = (void *)(real);
    }
    return ret;
}

inline void free_align(void* ptr) {
    void * real;

    if (ptr) {
        real = *((void **)(ptr)-1);
        free(real);
    }
}

#endif // __MALLOC_ALIGN_H__
